package org.zjkoye.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.micrometer.common.lang.Nullable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zjkoye.crm.common.PageResultBean;
import org.zjkoye.crm.common.Result;
import org.zjkoye.crm.entity.Customer;
import org.zjkoye.crm.service.CustomerService;
import org.zjkoye.crm.service.RegionService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjkoye
 * @since 2024-06-23
 */
@Controller
@RestController
@RequestMapping("/customer")

public class CustomerController {


    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private CustomerService customerService;

    @Autowired
    private RegionService regionService;

    @GetMapping("/pageList")
    @Operation(summary = "客户列表分页查询")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "pageSize", required = true, description = "每页显示的条数"),
            @Parameter(name = "currentPage", required = true, description = "要查询的页"),
            @Parameter(name = "name", description = "客户姓名", required = false)
    })
    public Result<PageResultBean<Customer>> pageList(@RequestParam int pageSize, @RequestParam int currentPage, @Nullable @RequestParam String name) {

        logger.info("pagesize:" +  pageSize , " currentPage:"+  currentPage , " name:"+  name );
        System.out.println("pagesize:"+  pageSize + " currentPage:"+  currentPage + " name:"+  name );
        IPage<Customer> page = customerService.pageList(pageSize, currentPage, name);
        if (page == null) {
            return Result.error("查询失败");
        }


        PageResultBean<Customer> pageResultBean = new PageResultBean<Customer>(page.getTotal(), page.getRecords());
        System.out.println("pagesize:"+  pageSize + " currentPage:"+  currentPage + " name:"+  name );
        logger.info(String.valueOf(pageSize));
        logger.info(Result.success(PageResultBean.getInstance(page.getTotal(), page.getRecords())).toString());
        return Result.success(PageResultBean.getInstance(page.getTotal(), page.getRecords()));

    }


    @PostMapping("/add")
    @Operation(summary = "添加客户")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> addUser(@RequestBody Customer user) {
        user.setStatus("新建");
        logger.info(user.toString());
        if(!regionService.isExist(user.getRegionName()))
            return Result.error("当前地区未添加到地区表中请联系管理员");
        Customer adduser = new Customer();
        adduser.setCustomerName(user.getCustomerName());
        adduser.setOldName(user.getOldName());
        adduser.setStatus(user.getStatus());
        adduser.setOperator(user.getOperator());
        adduser = user;
        return customerService.add(adduser);
    }
    @PutMapping("/update")
    @Operation(summary = "修改用户信息")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> update(@RequestBody Customer user) {
        boolean result = customerService.updateById(user);
        if (!result) {
            if(!regionService.isExist(user.getRegionName()))
                return Result.error("当前地区未添加到地区表中请联系管理员");
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除用户信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "用户id")
    })
    public Result<String> delete(@PathVariable int id) {
        boolean result = customerService.deleteById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
    @GetMapping("/getuserById")
    @Operation(summary = "根据id查询客户信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "客户id")
    })
    public Result<Customer> getUserById(@RequestParam int id) {
        Customer user = customerService.getCustomerById(id);
        if (user == null) {
            return Result.error("获取用户信息失败");
        }
        return Result.success(user);
    }

}
